package com.chao.common.util.redis;

import com.alibaba.fastjson.JSONObject;
import com.chao.common.config.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Set;

/**
 * 静态方法操作redis
 * chao
 * 2020-07-18
 */
@Component
public class SysRedis {

    @Autowired
    private RedisService redisService;

    private static RedisService redisStaticService;

    @PostConstruct
    public void init() {
        redisStaticService = redisService;
    }

    /**
     * 存储字符串
     */
    public static void setValue(String key,Object value){
        redisStaticService.setValue(key,value);
    }

    /**
     * 存储字符串 带有效期
     */
    public static void setValue(String key,Object value, long time){
        redisStaticService.setValue(key,value,time);
    }

    /**
     * 存储map
     */
    public static void setMap(String rkey,String key,Object value){
        redisStaticService.setMap(rkey,key,value);
    }

    /**
     * 存储list
     */
    public static void setList(String rkey,Object value){
        redisStaticService.setList(rkey,value);
    }


    /**
     * 获取字符串或java对象
     */
    public static <T>T getValue(String key){
        return redisStaticService.getValue(key);
    }

    /**
     * 获取map
     */
    public static JSONObject getMap(String rkey){
        return redisStaticService.getMap(rkey);
    }

    /**
     * 获取map
     */
    public static <T>T getMap(String rkey,String key){
        return redisStaticService.getMap(rkey,key);
    }

    /**
     * 获取list
     */
    public static <T> List<T> getList(String key){
        return redisStaticService.getList(key);
    }

    /**
     * 删除List 指定长度前的值
     */
    public static void deleteList(String key,Integer i){
        redisStaticService.deleteList(key,i);
    }

    /**
     * 清除key
     */
    public static void delete(String key){
        redisStaticService.delete(key);
    }

    /**
     * 设置key有效期 单位 秒
     */
    public static void setTime(String key,Integer time){
        redisStaticService.setTime(key,time);
    }

    /**
     * 判断key是否存在
     */
    public static Boolean hasKey(String key){
        return redisStaticService.hasKey(key);
    }

    /**
     * 模糊查询key列表
     */
    public static Set<String> matchKey(String key){
        return redisStaticService.matchKey(key);
    }
}
